package com.itheima.tlias.Service.impl;

import com.itheima.tlias.Mapper.DeptMapper;
import com.itheima.tlias.Mapper.EmpMapper;
import com.itheima.tlias.Service.DeptLogService;
import com.itheima.tlias.Service.DeptService;
import com.itheima.tlias.anno.Log;
import com.itheima.tlias.anno.MyLog;
import com.itheima.tlias.pojo.Dept;
import com.itheima.tlias.pojo.DeptLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
        private DeptLogService deptLogService;


    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }


    @Override
    @Transactional
    public void delete(Integer id) throws Exception {
        try {
            deptMapper.deleteById(id);
            empMapper.deleteByDeptId(id);
        } finally {
            log.info("开始记录日志");
            DeptLog deptLog = new DeptLog();
            deptLog.setCreateTime(LocalDateTime.now());
            deptLog.setDescription("此次操作执行例如解散id为" + id + "的部门");
            deptLogService.insert(deptLog);
        }
    }

    ;

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept selectById(Integer id) {
        Dept dept = deptMapper.selectById(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }


}
